package org.jtb.mensawotd;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Cleaner {
	static class Item {
		Pattern pattern;
		String replacement;

		Item(String p) {
			this(p, "");
		}

		Item(String p, String replacement) {
			this.pattern = Pattern.compile("(" + p + ")");
			this.replacement = replacement;
		}
	}

	private static final Item[] ITEMS = new Item[] { new Item("</?.+?>"),
			new Item("\\s{2,}", " "), new Item("^\\s+") };

	static String clean(String s) {
		boolean found;

		do {
			found = false;
			for (Item item : ITEMS) {
				StringBuffer sb = new StringBuffer();
				Matcher m = item.pattern.matcher(s);

				while (m.find()) {
					// Log.d("mensawotd", "replacing: \"" + m.group(1)
					// + "\" with: \"" + item.replacement + "\"");
					m.appendReplacement(sb, item.replacement);
					found = true;
				}
				m.appendTail(sb);
				s = sb.toString();
			}
		} while (found);

		return s;
	}
}
